/**
 * 
 */
package com.example.android_is2;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;

import com.example.android_is2.ListenerMap.LocationListenerMap;
import com.example.android_is2.ListenerMap.LocationListenerMapHere;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * @author KAIN-PC
 *
 */
public class MapsActivity extends MapActivity{

  private LocationManager locationManager = null;
  private LocationProvider netProvider = null;
  private LocationListenerMap myLocationListener = null;
  // BRIDGE SCAMBIO PARAMETRI TRA ACTIVITY
  private SingletonParametersBridge singleton_bridge = null;

  private BridgeLocation bridge = null;
  //------------------------------------
  // Location Manager = GPS Opp Network
  private final String defaultLocationManager = LocationManager.GPS_PROVIDER;
  //------------------------------------
  //Riferimento alla MapView
  private MapView mapView = null;

  //Riferimento al MapController
  private MapController mapController = null;

  //Riferimento al MyLocationOverlay
  private MyLocationOverlay myLocationOverlay = null;

  private MapItemizedOverlay itemizedoverlay = null;

  List<Overlay> overlays = null;

  // ONCREATE
  @Override
  protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.activity_map);
    
    singleton_bridge = SingletonParametersBridge.getInstance();
    bridge = (BridgeLocation) singleton_bridge.getParameter("bridge");
    //singleton_bridge.removeParameter("bridge");

    this.locationManager = bridge.getLocationManager();
    this.netProvider = bridge.getNetProvider();
    
    mapView = (MapView) findViewById(R.id.mapviewHere);
    mapView.setBuiltInZoomControls(true);

    // Otteniamo il riferimento al controller
    mapController = mapView.getController();
    // Rendiamo la mappa cliccabile e quindi ne permettiamo il pan
    mapView.setClickable(true);
    // Impostiamo la distanza dello Zoom a 12 (valori validi da 1 a 21).
    mapController.setZoom(7);
    // Aggiungiamo l'overlay sulla mappa della nostra posizione
    myLocationOverlay = new MyLocationOverlay(this, mapView);
    overlays = mapView.getOverlays();
    
    Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
    itemizedoverlay = new MapItemizedOverlay(drawable, this);
    
    // add this overlay to the MapView and refresh it
    overlays.add(myLocationOverlay);
    mapView.postInvalidate();
  }
  //------------------------------------
  // ONRESUME
  @Override
  protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    singleton_bridge = SingletonParametersBridge.getInstance();
    bridge = (BridgeLocation) singleton_bridge.getParameter("bridge");
    if (bridge == null)
    {
      //singleton_bridge.removeParameter("bridge");

      this.locationManager = bridge.getLocationManager();
      this.netProvider = bridge.getNetProvider();
    }

    // CONTROLLO LOCATION_MANAGER
    if (netProvider == null) {
      Log.i(MainActivity.TAG+getLocalClassName(),"NetProvider_OFF: "+netProvider.getName());
    } else {
      Log.i(MainActivity.TAG+getLocalClassName(),"NetProvider_ON: "+netProvider.getName());
      boolean netEnabled = locationManager.isProviderEnabled(defaultLocationManager);
      if (netEnabled) {
        Log.i(MainActivity.TAG+getLocalClassName(),"NetProvider_ENABLE_ON: "+netEnabled);
      } else {
        Log.i(MainActivity.TAG+getLocalClassName(),"NetProvider_ENABLE_OFF: "+netEnabled);
      }
      Location location = locationManager.getLastKnownLocation(defaultLocationManager);
      if (location != null) {
        LocationListenerMap.updateLocationData(location);

        int geoLat = (int) (location.getLatitude()*1E6);
        int geoLng = (int) (location.getLongitude()*1E6);

        Log.i(MainActivity.TAG+getLocalClassName(), "Lat: "+geoLat+" - Long: "+geoLng);
        GeoPoint point = new GeoPoint(geoLat, geoLng);
        if (point != null)
        {
          mapController.animateTo(point);
          OverlayItem overlayitem = new OverlayItem(point, LocationListenerMapHere.MAP_HERE, "Lat: "+Double.toString(LocationListenerMap.getRound(location.getLatitude(), 5))+
              "\nLng: "+Double.toString(LocationListenerMap.getRound(location.getLongitude(), 5)));

          itemizedoverlay.addOverlay(overlayitem);
          overlays.add(itemizedoverlay);
        }
      }
      myLocationOverlay.enableMyLocation();

      // Listener per acquisire cambiamenti Location
      myLocationListener = new LocationListenerMapHere(mapView,overlays,itemizedoverlay);
      // Registriamo il LocationListener al provider GPS
      locationManager.requestLocationUpdates(defaultLocationManager, 10000, 10, myLocationListener);
    }
  }
  //------------------------------------
  // ONPAUSE
  @Override
  protected void onPause() {
    super.onPause();
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (locationManager != null)
    {
      locationManager.removeUpdates(myLocationListener);
      myLocationOverlay.disableMyLocation();
    }
  }
  //------------------------------------
  @Override
  protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
  }
}
